<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category SendMailService.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/7/14  11:07
 * +--------------------------------------------------------+
 */

namespace App\Services;


use Illuminate\Support\Facades\Mail;

class SendMailService
{
    static public function send($type = 1, $title = '邮件', $content = '发送邮件', $email_arr, $send = [], $view = '')
    {
        switch ($type){
            case 1 : //发送纯文本
                Mail::raw($content,function ($message, $email_arr){
                   $emailArr = array_filter($email_arr);

                   foreach ($emailArr as $email){
                       $message->to($email)->subject('测试');
                   }
                });
                break;
                case 2 : //发送html
                    Mail::send($view,['send'=>$send], function ($message) use($send, $email_arr, $title){
                        $emailArr = array_filter($email_arr);   //去空值
                        foreach ($emailArr as $email){
                            $message->to($email)->subject($title);
                        }
                    });
                    break;
                default:
                    return ['code' => -1, 'message' => '类型错误'];
                    break;
        }

        // 判断邮件是否发送失败
        if(count(Mail::failures())) {
            return ['code' => -1, 'message' => '邮件发送失败'];
        }else{
            return ['code' => 0, 'message' => '邮件发送成功'];
        }
    }

}
